package com.zy.kotlinbaselibrary.net

import android.text.TextUtils
import com.zy.kotlinmvpapp.net.OkHttpConfig
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

/**
 * Created by zy on 2018/4/24.
 */

class RetrofitConfig private constructor(// 请求根路径
    private var baseUrl: String
) {

    private lateinit var retrofit: Retrofit

    private val okHttpConfig: OkHttpConfig = OkHttpConfig.okHttpConfig



    companion object {

        private val sProvider: NetProvider? = null


        private var retrofitConfig: RetrofitConfig? = null

        /**
         * @param baseUrl
         * @return
         */
        @JvmStatic
        fun getRetrofitConfigInstance(baseUrl: String): RetrofitConfig {
            if (retrofitConfig == null) {
                synchronized(RetrofitConfig::class.java) {
                    if (retrofitConfig == null) {
                        retrofitConfig = RetrofitConfig(baseUrl)
                    }
                }
            }
            return retrofitConfig!!
        }
    }

    /**
     * 初始化Retrofit对象
     * @param service
     * @param <T>
     * @return
    </T> */
    fun <T> configRetrofit(service: Class<T>): T {
        retrofit = Retrofit.Builder()
            .baseUrl(baseUrl)
            .client(okHttpConfig.getOkHttpClient())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
        return retrofit.create(service)
    }

    /**
     * 初始化Retrofit对象，可以更改某次请求baseUrl
     *
     * @param service
     * @param tempBaseUrl
     * @param <T>
     * @return
    </T> */
    fun <T> configRetrofit(service: Class<T>, tempBaseUrl: String): T {
        retrofit = Retrofit.Builder()
            .baseUrl(tempBaseUrl)
            .client(okHttpConfig.getOkHttpClient())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
        return retrofit.create(service)
    }

    /**
     * 用于okhttp的某个配置生成okhttpClineat。
     * @param service
     * @param okBuilder
     * @param <T>
     * @return
    </T> */
    fun <T> configRetrofit(service: Class<T>, okBuilder: OkHttpClient.Builder?): T {
        if (okBuilder == null) {
            throw NullPointerException("传入的OkHttpClient.Builder不能为空")
        }
        retrofit = Retrofit.Builder()
            .baseUrl(baseUrl)
            .client(okBuilder.build())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
        return retrofit.create(service)
    }

    /**
     * 重新更改baseUrl
     * @param baseUrl
     */
    fun setBaseUrl(baseUrl: String) {
        if (TextUtils.isEmpty(baseUrl)) {
            throw NullPointerException("传入的baseUrlt不能为空")
        }
        this.baseUrl = baseUrl
    }

    /**
     * 清空对象
     */
    fun clearInstance() {
        retrofitConfig = null
    }


}

